﻿using System;
using System.IO;
using Sustainalytics.CarbonData.BussinesLogic;
using Xunit;
using Sustainalytics.DataLayer.ConnectionStrings;
using System.Configuration;
using Sustainalytics.Entities.ConnectionStrings;

namespace Sustainalytics.CarbonData.Service.Tests
{
    public class ImportExcelFileTests
    {
        private string GetConnectionString()
        {
            var clientPlatformConnectionString = ConfigurationManager.ConnectionStrings["ClientPlatform"].ConnectionString;
            var connectionStringsManager = ConnectionStringsManager.GetInstance(ConnectionStringsRepository.GetInstance(clientPlatformConnectionString));

            var connectionString = connectionStringsManager.GetConnectionString(ConnectionStringType.ClientDW);

            return connectionString;
        }

        [Fact]
        public void SheetNotFoundTest()
        {
            var excelFileImport = new ExcelFileImport(GetConnectionString());

            ValidationResult result;

            using (var fileStream = File.OpenRead("./resources/upload template - No CD sheet.xlsx"))
            {
                result = excelFileImport.Validate(fileStream);
            }

            Assert.Equal(InputValidationErrors.NoSheetFound, result.FrameworkError);
        }

        [Fact]
        public void ColumnNotFoundTest()
        {
            var excelFileImport = new ExcelFileImport(GetConnectionString());

            ValidationResult result;

            using (var fileStream = File.OpenRead("./resources/upload template - Column mising.xlsx"))
            {
                result = excelFileImport.Validate(fileStream);
            }

            Assert.Equal(InputValidationErrors.ColumnNotFound, result.FrameworkError);
        }

        [Fact]
        public void ValidateExcelFileTest()
        {

            var excelFileImport = new ExcelFileImport(GetConnectionString());

            ValidationResult result;

            using (var fileStream = File.OpenRead("./resources/upload template.xlsx"))
            {
                result = excelFileImport.Validate(fileStream);
            }

            Assert.Equal(true, result.Success);
        }

        [Fact]
        public void ImportExcelFileTest()
        {
            var excelFileImport = new ExcelFileImport(GetConnectionString());
            
            ProcessResult result;

            using (var fileStream = File.OpenRead("./resources/upload template.xlsx"))
            {
                result = excelFileImport.Import(fileStream);
            }
            
            Assert.Equal(true, result.Success);
       }

    }
}
